package com.itheima.reggie.controller;

import com.itheima.reggie.commom.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Sea
 * @date 2023/3/1 19:32
 */
@Api(tags = "员工")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private HttpSession session;
    @ApiOperation("员工登录")
    //员工登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee loginEmployee) {
        ResultInfo resultInfo = employeeService.login(loginEmployee.getUsername(),
                loginEmployee.getPassword());
        return resultInfo;
    }

    @PostMapping("/employee/logout")
    public ResultInfo logout() {
        session.invalidate();
        return ResultInfo.success();
    }

    @GetMapping("/employee/find")
    public ResultInfo find(String name) {
        List<Employee> employeeList = employeeService.find(name);
        return ResultInfo.success(employeeList);
    }

    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee) {
        employeeService.save(employee);
        return ResultInfo.success();
    }

    @GetMapping("/employee/{id}")
    public ResultInfo findId(@PathVariable Long id) {
        Employee employee = employeeService.findId(id);
        return ResultInfo.success(employee);
    }
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee)
    {
        employeeService.update(employee);
        return ResultInfo.success();
    }
}